//
//  SKYInfoAcquisitionInterface.m
//  SkyonSDK
//
//  Created by 张二帅 on 2018/5/10.
//  Copyright © 2018年 Skyon. All rights reserved.
//

#import "SKYInfoAcquisitionInterface.h"
#import "SKYNetwork.h"
#import "SKYGlobalMacros.h"
#import "SKYInfoAcquisitionModel.h"
#import "SKYModel.h"
#import "SKYInfoServer.h"

@implementation SKYInfoAcquisitionInterface

+ (void)sendFistInstallInfo {
    [self sendPaskets:[SKYInfoAcquisitionModel fistInstallInfoAcquisitionPackets] completion:nil];
}

+ (void)sendAppLaunchInfo {
    [self sendPaskets:[SKYInfoAcquisitionModel appLaunchInfoAcquisition] completion:nil];
}

+ (void)sendCrashInfo:(NSDictionary *)crashDict {
    [self sendPaskets:[SKYInfoAcquisitionModel crashInfoAcquisitionWithCrashDict:crashDict] completion:nil];
}

+ (void)sendRegularInfo {
    [self sendPaskets:[SKYInfoAcquisitionModel regularInfoAcquisitionPackets] completion:nil];
}

+ (void)sendPageDidAppearWithPageTrackerModel:(SKYPageTrackerModel *)pageTrackerModel {
    [self sendPaskets:[SKYInfoAcquisitionModel pageDidAppearWithPageTrackerModel:pageTrackerModel] completion:nil];
}

+ (void)sendPageDidDisappearWithPageTrackerModel:(SKYPageTrackerModel *)pageTrackerModel {
    [self sendPaskets:[SKYInfoAcquisitionModel pageDidDisappearWithPageTrackerModel:pageTrackerModel] completion:nil];
}

+ (void)sendWebViewLoadWithCookieCheck_id:(NSString *)check_id lgn_ur:(NSString *)lgn_ur title:(NSString *)title urlString:(NSString *)urlString {
    if ([SKYInfoServer sharedInstance].config.webViewReportEnabled) {
        [self sendPaskets:[SKYInfoAcquisitionModel webViewLoadInfoAcquisitionWithCookieCheck_id:check_id cookieLgn_ur:lgn_ur title:title urlString:urlString] completion:nil];
    }
}

+ (void)sendCustomEventWithEvent:(NSString *)event eventCode:(NSString *)eventCode eventDescribe:(NSString *)eventDescribe;{
    [self sendPaskets:[SKYInfoAcquisitionModel eventPacketsWithEvent:event eventCode:eventCode eventDescribe:eventDescribe] completion:nil];
}

+ (void)getNetIpCompletion:(nullable void(^)(NSString *netIp))completionHandler {
    [[SKYNetwork config] GETURLString:@"http://pv.sohu.com/cityjson?ie=utf-8" completionHandler:^(NSData * _Nullable data, NSError * _Nullable error) {
        if (completionHandler) {
            NSString *dataString = data != nil ? [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : nil;
            NSArray *dataArray = (dataString != nil && dataString.length > 0 && [dataString rangeOfString:@"="].location != NSNotFound) ? [dataString componentsSeparatedByString:@"="] : nil;
            NSMutableString *jsonString = (dataArray != nil && dataArray.count > 1) ? [NSMutableString stringWithFormat:@"%@",dataArray.lastObject] : nil;
            if (jsonString != nil && [dataArray.lastObject rangeOfString:@";"].location != NSNotFound) {
                [jsonString deleteCharactersInRange:[jsonString rangeOfString:@";"]];
            }
            NSDictionary *dict = (jsonString != nil) ? [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil] : nil;
            NSString *ipString = (dict != nil && dict.count > 0) ? [dict valueForKey:@"cip"] : nil;
            completionHandler(ipString);
        }
        
    }];
}

/**
 发送采集信息
 
 @param packets 数据包
 @param completionHandler 完成回调，error != nil 时，包含错误信息    
 */
+ (void)sendPaskets:(NSArray *)packets completion:(nullable void(^)(NSError * _Nullable error))completionHandler {
    [[SKYNetwork config] POSTURLString:[SKYInfoServer sharedInstance].infoAcquisitionURLString packets:packets completionHandler:^(NSData * _Nullable data, NSError * _Nullable error) {
        if (completionHandler) {
            completionHandler(error);
        }
        id json = data != nil ? [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] : nil;
        if ([SKYInfoServer sharedInstance].logEnabled) {
            NSLog(@"%@",(error == nil ? json : [NSString stringWithFormat:@"errorInfo:%@",error.localizedDescription]));
        }
    }];
}

@end
